package com.sea.supply.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.codecrane.core.util.CommonUtils;
import com.sea.data.entity.OrderDetail;
import com.sea.data.service.OrderDetailService;
import com.sea.finance.controller.HttpUtils;
import com.sea.supply.entity.*;
import com.sea.supply.mapper.BillMapper;
import com.sea.supply.mapper.BuyMapper;
import com.sea.supply.service.*;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈鸿飞
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Override
    public int save(Bill bill) {
        return billMapper.insert(bill);
    }

    @Override
    public List<Bill> findByCnd(Bill bill) {
        return billMapper.findByCnd(bill);
    }

    @Override
    public List<Bill> findByRefusedId(Integer refusedId) {
        return billMapper.findByRefusedId(refusedId);
    }


    @Override
    public Integer delete(Integer id) {
        return billMapper.delete(id);
    }

    @Override
    public Bill findById(Integer id) {
        return billMapper.findById(id);
    }

    @Override
    public Integer usave(Bill bill) {
        return billMapper.update(bill);
    }

    @Override
    public Bill getImgInfo(String imgUrl) throws Exception {
        Bill bill = new Bill();
        com.aliyun.ocr_api20210707.Client client = createClient(("LTAI5tGgZyfTbsiKYZWNnPxy"),("l5IGwM4pbtWo8QK2dc7reuD5HgaaJR"));

        com.aliyun.ocr_api20210707.models.RecognizeInvoiceRequest recognizeAdvancedRequest = new com.aliyun.ocr_api20210707.models.RecognizeInvoiceRequest();
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        recognizeAdvancedRequest.url=imgUrl;

        com.aliyun.ocr_api20210707.models.RecognizeInvoiceResponse resp = client.recognizeInvoiceWithOptions(recognizeAdvancedRequest, runtime);
        com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
        JSONObject jsonObject = JSON.parseObject(com.aliyun.teautil.Common.toJSONString(resp));
        Integer statusCode = jsonObject.getInteger("statusCode");
        if(statusCode.equals(200)){
            String body = jsonObject.getString("body");
            System.out.println(body);
            JSONObject data =  JSON.parseObject(body).getJSONObject("data");
            System.out.println(data.toJSONString());
            JSONObject detail = data.getJSONObject("data");
            bill.setBillImg(imgUrl);
            bill.setSupplierName(detail.getString("sellerName"));
            bill.setBillCode(detail.getString("invoiceCode"));
            bill.setBillNumber(detail.getString("invoiceNumber"));
            bill.setBillPrice(detail.getDouble("invoiceAmountPreTax"));
            bill.setBillTax(detail.getDouble("invoiceTax"));
            bill.setBillMoney(detail.getDouble("totalAmount"));
        }else {
            throw new RuntimeException("图片异常请联系管理员!");
        }
        return bill;
    }
    public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.ocr_api20210707.Client(config);
    }
}
